package group.cyfc.studygo.dao;

import group.cyfc.studygo.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import javax.transaction.Transactional;

@Repository
public interface UserRepository extends JpaRepository<User,Long> {

    /**
     *
     * 用户加分
     * @param username
     * @param score
     */
    @Modifying
    @Transactional(rollbackOn = Exception.class)
    @Query(nativeQuery = true,value = "update user set score = score + :score where username = :username")
    void addScore(@Param("username")String username,@Param("score")Integer score);

    /**
     *
     * 管理员登录
     * @param username
     * @param password
     * @return User
     */
    @Query(nativeQuery = true,value = "select * from user where username = :username and password = :password and is_del = 0")
    User userLogin(@Param("username")String username,@Param("password")String password);

    /**
     *
     * 通过账号查找用户
     * @param username
     * @param is
     * @return User
     */
    User findUserByUsernameAndIsDel(String username,Boolean is);

    /**
     * 通过id查找用户
     * @param id
     * @param is
     * @return
     */
    User findByIdAndIsDel(Long id,Boolean is);
}
